<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-06-25 09:36
 * Modified Time: 2019-06-25 09:36
 * Version: 2.0
 */

namespace App\Modules\Employee\Controllers;

use App\Exceptions\AllValidatorException;
use App\Exceptions\NotFoundException;
use App\Lib\Facades\FLAuth;
use App\Lib\Utilities;
use App\Modules\DataCenter\Services\StatisticsService;
use App\Modules\Employee\Services\EmployeeService;
use App\Modules\Employee\Transformers\EmployeeTransformer;
use App\Modules\Employee\Transformers\GetBarberListCommentTransformer;
use App\Modules\Employee\Transformers\GetBarberRoyaltyTransformer;
use App\Modules\Employee\Transformers\GetBarberServiceListTransformer;
use App\Modules\Employee\Transformers\getModuleAndStoreStateTransformer;
use App\Modules\Employee\Transformers\GetStoreRestListTransformer;
use App\Modules\Employee\Transformers\GetUserInfoByRoleTransformer;
use App\Modules\Employee\Transformers\ScheduleTransformer;
use App\Modules\Employee\Validators\EmployeeControllerValidator;
use App\Modules\Order\Services\OrderCommentService;
use App\Modules\Store\Services\StoreService;
use App\Modules\System\Services\CommonService;
use Flower\Core\Http\BaseController;
use Flower\Core\Http\Request;

/**
 * Class EmployeeController 理发师和店长信息表
 * @package App\Modules\Employee\Controllers
 * Author: Happy dong
 * Created Time: 2019-06-25 10:28
 */
class EmployeeController extends BaseController
{
	private $employeeService;
	private $orderCommentService;
	private $commonService;
	private $storeService;
	private $statisticsService;

	public function __construct(EmployeeService $employeeService, OrderCommentService $orderCommentService, CommonService $commonService, StoreService $storeService, StatisticsService $statisticsService
	)
	{
		$this->employeeService = $employeeService;
		$this->orderCommentService = $orderCommentService;
		$this->commonService = $commonService;
		$this->storeService = $storeService;
		$this->statisticsService = $statisticsService;
	}

	public function getRules()
	{
		return EmployeeControllerValidator::validator();
	}

	/**
	 * 理发师或者店长注册
	 * @param Request $request
	 * @return mixed
	 */
	public function registerUser(Request $request)
	{
		$check = $this->commonService->checkSmsCode($request->mobile, 0, $request->captcha);
		if (!$check) {
			return $this->error("手机验证码错误！");
		}
		$userInfo = $this->employeeService->registerEmployee($request->mobile, $request->password);
		if (!$userInfo) return $this->error("用户注册失败");
		return $this->display((new EmployeeTransformer($userInfo))->transform(), "用户注册成功");
	}

	/**
	 * 获取理发师个人信息
	 * @param Request $request
	 * @return mixed
	 */
	public function getEmployeeInfo(Request $request)
	{
		$user_id = FLAuth::id();
		$store_id = $request->input('store_id');
		$data = $this->employeeService->getEmployeeInfo($user_id,$store_id);
		return $this->display($data, '查询成功');
	}

	/**
	 * 获取当前的理发师的县令币余额
	 * @param Request $request
	 * @return mixed
	 * @throws NotFoundException
	 */
	public function getEmployeeCoinInfo(Request $request){
		$result = $this->employeeService->getBarberCoinInfo(FLAuth::id());
		return $this->handleApiResult($result);
	}



	/**
	 * 修改理发师个人信息
	 * @param Request $request
	 * @return mixed
	 */
	public function updateEmployeeInfo(Request $request)
	{
		$user_id = FLAuth::id();
		$this->employeeService->updateEmployeeInfo($user_id, $request->nick, $request->portrait, $request->introduction, $request->mobile, $request->wechat);
		return $this->success('操作成功');
	}


	/**
	 * 添加理发师到店铺
	 * @param Request $request
	 * @return mixed
	 * @throws AllValidatorException
	 * @throws NotFoundException
	 * @throws \App\Exceptions\DbException
	 */
	public function addBarber(Request $request)
	{
		$barberId = FLAuth::id();
		$userStore = $this->employeeService->AddNewBarber($barberId, $request->store_id);
		$roleInfo = $this->employeeService->getRoleInfo($barberId, $request->store_id, $userStore->rid);
		return $this->display((new EmployeeTransformer(null, $roleInfo))->transform(), "理发师添加成功");
	}


	/**
	 * 理发师排班设置
	 * @param Request $request
	 * @return mixed
	 * @throws NotFoundException
	 * @author Happy dong
	 * @modifiedTime 2019-08-09 17:41
	 */
	public function setWorkDay(Request $request)
	{
		$days = json_decode($request->days, true);
		$list = $this->employeeService->setWorkDay(FLAuth::id(), $request->store_id, $request->date, $days);
		if (empty($list)) return $this->error("日程安排失败");
		return $this->success("日程安排成功");
	}


	/**
	 * 依据月份查询日程安排
	 * @param Request $request
	 * @return mixed
	 * @author Happy dong
	 * @modifiedTime 2019-08-09 17:41
	 */
	public function getScheduleByMonth(Request $request)
	{
		$list = $this->employeeService->getScheduleByMonth(FLAuth::id(), $request->store_id, $request->date);
		return $this->display((new ScheduleTransformer($list))->transform());
	}

	/**
	 * 店长查询理发师休假情况
	 * @param Request $request
	 * @return mixed
	 */
	public function getStoreRestList(Request $request)
	{
		$list = $this->employeeService->getStoreRestList($request->store_id, $request->date, true);
		return $this->display((new GetStoreRestListTransformer($list))->transform());
	}

	/**
	 *  关注理发师
	 * @param Request $request
	 * @return mixed
	 * @throws \App\Exceptions\AllValidatorException
	 */
	public function likeUser(Request $request)
	{
		$this->employeeService->likeUser($request->all());
		return $this->success("操作成功");
	}

	/**
	 * 获取某店下所有理发师 wx端
	 * @param Request $request
	 * @return mixed
	 * @throws NotFoundException
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function getStoreUserList(Request $request)
	{
		$store_id = $request->input('store_id');

		$list = $this->employeeService->getStoreUserList($store_id);

		return $this->display($list, '查询成功');
	}

	/**
	 * 获取理发师详情页面信息
	 * @param Request $request
	 * @return mixed
	 * @throws NotFoundException
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function getBarberInfo(Request $request)
	{
		$Info = $this->employeeService->getBarBerInfo($request->all());
		return $this->display($Info, "成功获取理发师信息");
	}

	/**
	 * 获取理发师评论列表
	 * @param Request $request
	 * @return mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function getBarberListComment(Request $request)
	{
		$memberId = FLAuth::id();
		$pageSize = isset($request->pageSize) ? $request->pageSize : 10;
		$orderId = isset($request->order_id) ? $request->order_id : 0;
		$Info = $this->orderCommentService->getBarberListComment($request->store_id, $request->barber_id, $orderId, $pageSize, $memberId);
		return $this->display((new GetBarberListCommentTransformer($Info))->transformer(), "成功获取理发师评论信息");
	}

	/**
	 * 获取理发师服务列表 小程序
	 * @param Request $request
	 * @return mixed
	 * @throws NotFoundException
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function getBarberServiceList(Request $request)
	{
		$is_live = $request->input('is_live',0);
		$list = $this->employeeService->serviceList($request->barber_id, $request->store_id);
		return $this->display((new GetBarberServiceListTransformer($list,$is_live))->transformer(), "成功获取理发师服务列表信息");
	}



	/**
	 * 直播间 获取理发师服务列表 小程序
	 * @param Request $request
	 * @return mixed
	 * @throws NotFoundException
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function getBarberServiceListForLive(Request $request)
	{
		$list = $this->employeeService->serviceList($request->barber_id, $request->store_id);
		return $this->display((new GetBarberServiceListTransformer($list,1))->transformer(), "成功获取理发师服务列表信息");
	}

	/**
	 * 获取理发师的服务列表
	 * @param Request $request
	 * @return mixed
	 * @throws NotFoundException
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function getBarberServiceListApp(Request $request)
	{
		$is_live = $request->input('is_live',0);
		$barber_id = $is_live ? FLAuth::id() : $request->barber_id;
		$list = $this->employeeService->serviceList($barber_id, $request->store_id);
		return $this->display((new GetBarberServiceListTransformer($list,$is_live))->transformer(), "成功获取理发师服务列表信息");
	}


	/**
	 * App端获取理发师服务提成列表 增加会员卡的团购的支付方式提成
	 * @param Request $request
	 * @return mixed
	 * @throws NotFoundException
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function getBarberRoyalty(Request $request)
	{
		$list = $this->employeeService->serviceList($request->user_id, $request->store_id, 0);
		$baseSalary = $this->storeService->getBaseSalaryByUserId($request->store_id,$request->user_id,config('constant.role.barber'));
		$storePayment = $this->storeService->getStorePaymentRoyalty($request->store_id);
		return $this->display((new GetBarberRoyaltyTransformer($list,$baseSalary,$storePayment))->transform());
	}


	/**
	 * 店铺角色切换接口
	 * @param Request $request
	 * @return mixed
	 * @throws \App\Exceptions\NotFoundException
	 * @author Happy dong
	 * @modifiedTime 2019-08-01 13:38
	 */
	public function getUserInfo(Request $request)
	{
		$userId = FLAuth::id();
		$roleType = $request->input('role_type',0);
		$userInfo = $this->employeeService->getUserBaseInfo($userId);
		$roleInfo = $this->employeeService->getRoleInfo($userId, $request->store_id, $roleType);
		;
		$storeList = $this->employeeService->getStoreListByUserId($userId);
		$currentStoreInfo = $this->storeService->getStoreInfoById($request->store_id);
		list($roleList,$currentRoleType) = array_values($this->employeeService->getUserStoreRoleList($currentStoreInfo->id,$userId,$roleType));

		if ($currentRoleType == config('constant.role.manager')) {
			$isWorkedOpened = $this->storeService->checkIsOnline($request->store_id);
		}else{
			$isWorkedOpened = $this->employeeService->isWorkedById($userId, $request->store_id);
		}

		$staticsByStoreOrBarber = $this->statisticsService->getStoreOrBerberControlHeaderInfo($request->store_id, $userId, $currentRoleType);
		$roleModule = $this->employeeService->getModuleByRoleId($currentRoleType);
		return $this->display((new EmployeeTransformer($userInfo, $roleInfo, $currentStoreInfo, $storeList,$staticsByStoreOrBarber, $isWorkedOpened, $currentRoleType,$roleList,$roleModule))->transform(), "获取用户信息成功");
	}

	/**
	 * 切换角色身份
	 * @param Request $request
	 * @return mixed
	 * @throws NotFoundException
	 */
	public function getModuleAndStoreState(Request $request){
		$userId = FLAuth::id();
		$currentRoleInfo = $this->employeeService->getUserStoreRoleList($request->store_id,$userId,$request->role_type);
		$currentRoleType = $currentRoleInfo['currentRoleType'];
		if ($currentRoleType == config('constant.role.manager')) {
			$isWorkedOpened = $this->storeService->checkIsOnline($request->store_id);
		}else{
			$isWorkedOpened = $this->employeeService->isWorkedById($userId, $request->store_id);
		}
		$staticsByStoreOrBarber = $this->statisticsService->getStoreOrBerberControlHeaderInfo($request->store_id, $userId, $currentRoleType);
		$roleModule = $this->employeeService->getModuleByRoleId($currentRoleType);
		return $this->display((new getModuleAndStoreStateTransformer($roleModule,$staticsByStoreOrBarber,$currentRoleType,$isWorkedOpened))->transform());
	}
	/**
	 * 获取当前店铺的所有理发师
	 * @param Request $request
	 * @return mixed
	 * @throws \App\Exceptions\NotFoundException
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 * @author Happy dong
	 * @modifiedTime 2019-08-01 13:38
	 */
	public function getBarberList(Request $request)
	{
		$pageSize = isset($request->page_size) ? $request->page_size : 5;
		$list = $this->employeeService->getStoreBarberList($request->store_id, $pageSize);
		return $this->display($list);
	}

	/**
	 * 查询店铺某角色的用户列表
	 * @param Request $request
	 * @return mixed
	 * @throws NotFoundException
	 */
	public function getUserInfoByRole(Request $request)
	{
		$info = $this->employeeService->getUserInfoByRole($request->role, $request->store_id,FLAuth::id(),$request->have_self);
		return $this->display((new GetUserInfoByRoleTransformer($info))->transform());
	}


	/**
	 * 理发师基本工资和服务提成
	 * @param Request $request
	 * @return mixed
	 * @throws AllValidatorException
	 * @throws \App\Exceptions\DbException
	 */
	public function setBarberRoyalty(Request $request)
	{
		$user_id = $request->input('user_id');
		$store_id = $request->input('store_id');
		$salary = $request->input('salary');
		$royalty = $request->input('royalty');
		$royalty = json_decode($royalty, true);

		if(empty($royalty)){
			throw new AllValidatorException(['msg'=>'提成参数royalty，数据格式错误，数据格式为json串']);
		}

		$this->employeeService->setBarberRoyalty($user_id, $store_id, $salary, $royalty);

		return $this->success("提成设置成功！");
	}


	public function getEmployeeProfile(Request $request)
	{
		$user_id = FLAuth::id();
		$data = $this->employeeService->getEmployeeProfile($user_id);

		return $this->display($data);
	}

	public function updateEmployeeProfile(Request $request)
	{
		$user_id = FLAuth::id();
		$profile_picture = $request->input('profile_picture');
		$profile_video = $request->input('profile_video');
		$this->employeeService->updateEmployeeProfile($user_id, $profile_picture, $profile_video);

		return $this->success('操作成功');
	}

	/**
	 * 以OSS地址方式上传理发师LOGO
	 * @param Request $request
	 * @return mixed
	 * @throws
	 */
	public function updateEmployeePortrait(Request $request)
	{
		$user_id = FLAuth::id();
		$portrait = $request->input('portrait');
		$this->employeeService->updateEmployeePortrait($user_id, $portrait);
		return $this->success("操作成功");
	}

	/**
	 * 删除理发师
	 * @param Request $request
	 * @return mixed
	 */
	public function delBarber(Request $request)
	{
		$user_id = $request->input('user_id');
		$store_id = $request->input('store_id');
		$this->employeeService->delBarber($store_id, $user_id);
		return $this->success("操作成功");
	}

	/**
	 * 获取理发师粉丝列表
	 */
	public function getBarberFansList(Request $request)
	{
		$user_id = FLAuth::id();
		$store_id = $request->input('store_id');
		$pageSize = $request->input('pageSize', 10);
		$data = $this->employeeService->getBarberFansList($user_id, $store_id, $pageSize);
		return $this->display($data);
	}

	/**
	 * 获取理发师今日粉丝数
	 */
	public function getTodayBarberFansNum(Request $request)
	{
		$user_id = FLAuth::user();
		$store_id = $request->input('store_id');
		$data = $this->employeeService->getTodayBarberFansNum($user_id, $store_id);
		return $this->display($data);
	}

	/**
	 * 排班任务
	 * @author Happy dong
	 * @modifiedTime 2019-09-18 17:15
	 */
	public function rewriteSchedule(){
		$res = $this->employeeService->rewriteSchedule();
		return $this->success();
	}


	public function consumedCoin(Request $request){
		$userId = FLAuth::id();
		$coin = $request->input('coin');
		$coin = empty($coin) ? config('constant.live_top_coin') : $coin;
		$result = $this->employeeService->consumedCoin($userId,$coin);
		return $this->handleApiResult($result);
	}
}
